#include <cstdio>
#include <cstring>
#include <queue>
//Edmond-karp
using namespace std;
const int N = 10010;

struct Edge {
	int u, v, nxt, cap, flow;
} e[N * 10];
int cnt = 0, heads[N];
inline void add(int u, int v, int c) {
	e[cnt].v = v;
	e[cnt].u = u;
	e[cnt].cap = c;
	e[cnt].nxt = heads[u];
	heads[u] = cnt++;
}
inline void init(int n) {
	for (int i = 1; i <= n; ++i) {
		heads[i] = -1;
	}
}
int n, m, s, t;
int a[N], p[N];
int maxflow(int s, int t) {
	int flow = 0;
	while (true) {
		memset(a, 0, sizeof(a));
		queue< int > q;
		q.push(s);
		a[s] = 0x7fffffff;
		while (!q.empty()) {
			int x = q.front();
			q.pop();
			for (int i = heads[x]; ~i; i = e[i].nxt) {
				if (!a[e[i].v] && e[i].cap > e[i].flow) {
					p[e[i].v] = i;
					a[e[i].v] = min(a[x], e[i].cap - e[i].flow);
					q.push(e[i].v);
				}
			}
			if (a[t]) break;
		}
		if (!a[t]) break;
		for (int i = t; i != s; i = e[p[i]].u) {
			e[p[i]].flow += a[t];
			e[p[i] ^ 1].flow -= a[t];
		}
		flow += a[t];
	}
	return flow;
}
int main() {
	// freopen("testdata.in", "r", stdin);
	// freopen("testdata.out", "w", stdout);
	scanf("%d%d%d%d", &n, &m, &s, &t);
	init(n);
	int t1, t2, t3;
	for (int i = 1; i <= m; ++i) {
		scanf("%d%d%d", &t1, &t2, &t3);
		add(t1, t2, t3);
		add(t2, t1, 0);
	}
	printf("%d", maxflow(s, t));
	return 0;
}